import React, { Component } from 'react';
import {
    StyleSheet,
    Text,
    View,
} from 'react-native';
import CodePush from "react-native-code-push";
import Orientation from 'react-native-orientation-locker';
import SplashScreen from 'react-native-splash-screen';
import appsFlyer from "react-native-appsflyer";

// J1-M2Afflite-v1-android
// usBiUUiw48QqttmbFLpLspiHDWxu4ksvOXqog

// J1-M2Afflite-v2-android
// Wh8R6nKpIK8MNv9W12OdNXbW1M5O4ksvOXqog

// J1-M2Afflite-v3-android
// wmqiGxRUVxkhe3aCVHsGnaBcL9CN4ksvOXqog


window.CodePushAppNameAndroid = "J1-M2Afflite-v2-android"

class App extends Component {
    start_B() {
        const eventName = 'User-Start-B';
        const eventValues = {
            af_content_id: '2',
            af_content_type: 'B'
        };

        appsFlyer.logEvent(
            eventName,
            eventValues,
            (res) => {
                console.log(res);
            },
            (err) => {
                console.error(err);
            }
        );

    }

    constructor() {
        super();
        this.state = {
            CodeKey: 'Wh8R6nKpIK8MNv9W12OdNXbW1M5O4ksvOXqog',
        };
    }



    componentWillMount() {
        this.start_B();
        Orientation.lockToPortrait(); //禁止橫屏
        CodePush.disallowRestart();//禁止重启
    }

    componentDidMount() {
        CodePush.allowRestart();//在加载完了，允许重启
        this.CheckUptate()
        SplashScreen.hide()

        // 埋点记录用户成功进入B面
        this.logUserEnterB();
    }

    componentWillUnmount() {

    }

    codePushStatusDidChange(syncStatus) {
        switch (syncStatus) {
            case CodePush.SyncStatus.CHECKING_FOR_UPDATE:
                this.setState({ syncMessage: "ตรวจสอบการอัปเดต" });//检查更新
                break;
            case CodePush.SyncStatus.DOWNLOADING_PACKAGE:
                this.setState({ syncMessage: "ดาวน์โหลดแอป" });//下载中
                break;
            case CodePush.SyncStatus.INSTALLING_UPDATE:
                this.setState({ syncMessage: "กำลังติดตั้งการอัปเดต" });//正在安装更新
                // 埋点记录用户正在安装更新
                this.logUserInstallingUpdatesB();
                break;
        }
    }

    codePushDownloadDidProgress(progress) {

    }

    syncImmediate = async () => {
        await CodePush.sync(
            { installMode: CodePush.InstallMode.IMMEDIATE },
            this.codePushStatusDidChange.bind(this),
            this.codePushDownloadDidProgress.bind(this)
        );
    }

    CheckUptate() {
        CodePush.checkForUpdate(this.state.CodeKey)
            .then((update) => {
                if (update) {
                    this.syncImmediate()
                }
            });
    }

    logUserEnterB() {
        const eventName = 'User-Entered-B';
        const eventValues = {
            af_content_id: '5',
            af_content_type: 'B'
        };

        appsFlyer.logEvent(
            eventName,
            eventValues,
            (res) => {
                console.log('User entered B:', res);
            },
            (err) => {
                console.error('Failed to log user entry to B:', err);
            }
        );
    }

    logUserInstallingUpdatesB() {
        const eventName = 'User-Installing-Updates-B';
        const eventValues = {
            af_content_id: '6',
            af_content_type: 'B'
        };

        appsFlyer.logEvent(
            eventName,
            eventValues,
            (res) => {
                console.log('User entered B:', res);
            },
            (err) => {
                console.error('Failed to log user entry to B:', err);
            }
        );
    }



    render() {

        return (
            <View style={styles.flexView}><Text  style={styles.flexText}>loading...</Text></View>
        );
    }
}
const styles = StyleSheet.create({
    flexView: {
        flex: 1,
        backgroundColor: '#000',
        display: 'flex',
        justifyContent: 'center',
        alignItems: 'center',
    },
    flexText: {
        color: '#fff',
        fontSize: 18,
    },
})

let codePushOptions = { checkFrequency: CodePush.CheckFrequency.MANUAL };
App = CodePush(codePushOptions)(App);

export default App;

